package com.quanjiaolian.findins.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.quanjiaolian.findins.service.FindInsService;
import com.quanjiaolian.findins.serviceimpl.FindInsServiceImpl;
import com.quanjiaolian.tools.LocationUtils;
import com.quanjiaolian.tools.ResponseToJson;
/**
 * 根据用户需求动态加载教练列表
 * @author Herson_Li
 *
 */
@SuppressWarnings("serial")
public class FindInsAction extends HttpServlet {

	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(FindInsAction.class.getName());
	
	Map<String,Object> resultMap = new HashMap<String,Object>();
	FindInsService fis = new FindInsServiceImpl();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {


			this.doPost(request, response);
	}
	/**
	 * 根据用户需求动态加载教练列表
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try{
			String firstSize = request.getParameter("firstSize");
			String maxSize = request.getParameter("maxSize");
			String sex = request.getParameter("sex");
			String teach_year = request.getParameter("teach_year");
			String pay_min = request.getParameter("pay_min");
			String pay_max = request.getParameter("pay_max");
			String audit_results = request.getParameter("audit_results");
			String latitude = request.getParameter("latitude");
			String longitude = request.getParameter("longitude");
			String type = request.getParameter("type");
			String st_id = request.getParameter("st_id");
			double minLat = 0;
			double minLng = 0;
			double maxLat = 0;
			double maxLng = 0;
			if("1".equals(type)){
				double[] db = LocationUtils.getAround(Double.valueOf(latitude),Double.valueOf(longitude),10000);//获得以用户为中心 半径为10km以内的区域坐标
				minLat = db[0];
				minLng = db[1];
				maxLat = db[2];
				maxLng = db[3];
			}
			
			if(firstSize == null || "1".equals(firstSize)){
				firstSize = "1";
			}
			if(maxSize == null){
				maxSize = "5";
			}
			List<Object> list = fis.queryInsList(Integer.parseInt(firstSize),Integer.parseInt(maxSize), sex, teach_year, pay_min, pay_max, audit_results, latitude,longitude,minLat,minLng,maxLat, maxLng,Integer.parseInt(type),st_id);
			if(list != null && list.size()>0){
				resultMap.put("result",list);
			}else{
				resultMap.put("result","no result");
			}
		}catch (Exception e) {
			resultMap.put("result","Error");
			logger.info(e);
			e.printStackTrace();
		}
		ResponseToJson.responseOutWithJson(response, resultMap);
		
	}

}
